﻿/**
*   @Copyright 2007 Vesatile Informatique
*
*   Version: 1.00
*   Creation Date:01.04.2007
*   Change Date: 23.02.2009
*   Creators Name: Anourak Bouavong
*   Last Edited By: Kalyan Bag
*   Description:[Tree Manager]
*/

//#region
/// <summary>
/// Tree Main class
/// </summary>
function MainTree() {
    this.TreeObj = null;
    this.JSON = null;

    this.NodeId = "ID";
    this.NodeName = "NAME";
    this.IS_TREE_OPEN = "IS_TREE_OPEN";

    this.idMap = "";
    this.uniqueIndex = 0;

    this.MainHTML = "";

    this.ulNode = "ul";
    this.liNode = "li";
    this.spanNode = "span";
    this.ulClass = "jqueryFileTree";
    this.ieUnseletedAble = 'unselectable="on"';
    this.attrId = "id";
    this.attrClass = "class";
    this.directoryClass = "directory";
    this.directorycollapsedClass = "collapsed";
    this.directoryexpandedClass = "expanded";
    this.fileClass = "file";
    this.imageClass = "image";
    this.textClass = "text";

    this.UIDMap = new Array();

    this.onCollapsedDelete = true;

    this.WO = null;

    this.clickFunction = function (e) { treeNodeBind.Click(e) };

    this.DoubleClickFunction = function (e) { treeNodeBind.DoubleClick(e) };

    this.OnMouseOverFunction = function (e) { treeNodeBind.OnMouseOver(e) };

    this.OnMouseOutFunction = function (e) { treeNodeBind.OnMouseOut(e) };
};
//#endregion

//#region
/// <summary>
/// Operation on Click
/// </summary>
MainTree.prototype.Click = function (e) {
    // IE event
    var workingObject = e.srcElement;
    if (workingObject == undefined) {   // W3C event
        workingObject = e.target;
    }

    ///set clicked Object
    this.WO = workingObject;

    var parentNode = workingObject.parentNode;

    ///on collapse operation
    if (IsClassNameExist(parentNode, this.directoryexpandedClass)) {
        if (this.onCollapsedDelete) {
            parentNode.childNodes[2].innerHTML = "";
        } else {
            parentNode.childNodes[2].style.display = 'none';
        }
        parentNode.className = GetRemovedClass(parentNode, this.directoryexpandedClass)
        parentNode.className = GetAddedClass(parentNode, this.directorycollapsedClass);

        this.onCollapseNode(this.WO);
    } else if (IsClassNameExist(parentNode, this.directorycollapsedClass)) {    ///on Expand operation
        if (!this.onCollapsedDelete) {  
            parentNode.childNodes[2].innerHTML = "";
        } else {
            parentNode.childNodes[2].style.display = '';
        }
        parentNode.className = GetRemovedClass(parentNode, this.directorycollapsedClass)
        parentNode.className = GetAddedClass(parentNode, this.directoryexpandedClass);

        this.onExpandNode(this.WO);
    }
};
//#endregion

//#region
/// <summary>
/// Operation on DoubleClick
/// </summary>
MainTree.prototype.DoubleClick = function (e) {

};
//#endregion

//#region
/// <summary>
/// Operation on MouseOver
/// </summary>
MainTree.prototype.OnMouseOver = function (e) {

};
//#endregion

//#region
/// <summary>
/// Operation on MouseOut
/// </summary>
MainTree.prototype.OnMouseOut = function (e) {

};
//#endregion

//#region
/// <summary>
/// Operation on RightClick
/// </summary>
MainTree.prototype.OnRightClick = function (obj) {

};
//#endregion

//#region
/// <summary>
/// Operation on ExpandNode
/// </summary>
MainTree.prototype.onExpandNode = function (obj) {

};
//#endregion

//#region
/// <summary>
/// Operation on CollapseNode
/// </summary>
MainTree.prototype.onCollapseNode = function (obj) {

};
//#endregion

//#region
/// <summary>
/// Operation on Rename 
/// </summary>
MainTree.prototype.onRenameNode = function (obj) {

};
//#endregion

//#region
/// <summary>
/// Operation on Delete 
/// </summary>
MainTree.prototype.OnDeleteNode = function (obj) {

};
//#endregion

//#region
/// <summary>
/// Operation on Add
/// </summary>
MainTree.prototype.AddNode = function (object, JSON) {
    //set global variables
    this.MainHTML = "";
    this.UIDMap = new Array();

    ///JSON to Tree HTML
    var TreeHTML = this._getTreeHTML(JSON, object.getAttribute("id"));
    ///insert HTML
    object.childNodes[2].innerHTML = [object.childNodes[2].innerHTML, TreeHTML].join("");
    ///Bind newly add tree node
    this.BindTree();

    this.WO = object;

    this.onAddNode(this.WO);
};
//#endregion

//#region
/// <summary>
/// Operation on Add
/// </summary>
MainTree.prototype.DeleteNode = function (object) {
    this.WO = object;

    ///get parent Node
    var parentNode = object.parent;
    ///remove object
    parentNode.removeChild(object);

    this.OnDeleteNode(this.WO);
};
//#endregion


//#region
/// <summary>
/// Operation on Add user function
/// </summary>
MainTree.prototype.onAddNode = function (obj, JSON) {
    
};
//#endregion

//#region
/// <summary>
/// Load the tree
/// </summary>
MainTree.prototype.LoadTree = function (id, JSON) {
    ///set those variables
    this.idMap = this.TreeObj.getAttribute("id");
    this.MainHTML = "";
    this.UIDMap = new Array();

    ///get the tree html
    var treeHTML = this.GetTreeHTML(JSON, this.idMap);

    var workingObject = document.getElementById(id);
    workingObject.innerHTML = treeHTML;

    ///bind the tree
    this.BindTree();
};
//#endregion

//#region
/// <summary>
/// Bind the tree
/// </summary>
MainTree.prototype.BindTree = function () {
    ///iterate the UI Id array
    for (var id in this.UIDMap) {
        var liNode = document.getElementById(id);
        var childNode = liNode.childNodes;
        ///bind for folder node
        if (childNode.length > 2) {
            var firstSpan = childNode[0];
            var secondSpan = childNode[1];

            listen("click", firstSpan, this.clickFunction);
            listen("click", secondSpan, this.clickFunction);
        } else {    ///bind for file node
            var firstSpan = childNode[0];

            listen("click", firstSpan, this.clickFunction);
        }
    }
};
//#endregion

//#region
/// <summary>
/// Get working JSON
/// </summary>
MainTree.prototype.LoadJSON = function (id) {
    ///check the tree parent node id
    /// if exist of not
    if (id == this.TreeObj.getAttribute("id")) {
        return this.JSON;
    } else {

    }
};
//#endregion

//#region
/// <summary>
/// Generate tree outer HTML
/// </summary>
MainTree.prototype.GetTreeHTML = function (JSON, id) {
    ///set the global variable
    this.MainHTML = "";

    ///generate the outer html
    var treeHTML = ["<", this.ulNode, " ", this.attrClass, "=\"", this.ulClass, "\" ", this.ieUnseletedAble, ">"].join("");
    ///get inner html
    treeHTML += this._getTreeHTML(JSON, id);
    treeHTML += ["</", this.ulNode, ">"].join("");

    return treeHTML;
};
//#endregion

//#region
/// <summary>
/// Generate tree inner HTML
/// </summary>
MainTree.prototype._getTreeHTML = function (JSON, preId) {
    ///iterate the JSON 
    for (var item in JSON) {
        ///get unique UI ID
        var id = this.GetUniqueId(preId, JSON[item][this.NodeId]);
        ///get the name of the node
        var name = JSON[item][this.NodeName];
        ///get the tree status 
        var is_tree_open = JSON[item][this.IS_TREE_OPEN];

        var objectNodeFlag = false;

        ///check JSON node type 
        ///if it is object or not 
        if (typeof (JSON[item]) == "object") {
            if (name == undefined) {
                name = item;
                objectNodeFlag = true;
            }

            ///check JSON sub node type 
            ///if it is object or not 
            var flag = false;
            for (var subItem in JSON[item]) {
                if (typeof (JSON[item][subItem]) == "object") {
                    flag = true;
                    break;
                }
            }

            ///HTML for innerHTML of the tree node
            if (flag) {
                ///update node id into and global array
                this.UIDMap[id] = id;

                ///html for those node which has only sub JSONs
                this.MainHTML += ["<", this.liNode, " ", this.attrClass, "=\"", this.directoryClass, " ", this.directoryexpandedClass, "\" ", this.ieUnseletedAble, " ", this.attrId, "=\"", id, "\"><", this.spanNode, " ", this.ieUnseletedAble, " ", this.attrClass, "=\"", this.imageClass, "\"></", this.spanNode, "><", this.spanNode, " ", this.attrClass, "=\"", this.textClass, "\" ", this.ieUnseletedAble, ">", name, "</", this.spanNode, ">"].join("");
            } else {
                if (!objectNodeFlag) {
                    ///update node id into and global array
                    this.UIDMap[id] = id;

                    ///html for those node which has no sub JSON
                    this.MainHTML += ["<", this.liNode, " ", this.attrClass, "=\"", this.fileClass, "\" ", this.ieUnseletedAble, " ", this.attrId, "='", id, "'><", this.spanNode, " ", this.attrClass, "=\"", this.textClass, "\" ", this.ieUnseletedAble, ">", name, "</", this.spanNode, ">"].join("");
                } else {
                    ///update node id into and global array
                    this.UIDMap[id] = id;

                    ///html for those node which has only sub JSONs and also other properties
                    this.MainHTML += ["<", this.liNode, " ", this.attrClass, "=\"", this.directoryClass, " ", this.directoryexpandedClass, "\" ", this.ieUnseletedAble, " ", this.attrId, "=\"", id, "\"><", this.spanNode, " ", this.attrClass, "=\"", this.imageClass, "\" ", this.ieUnseletedAble, "></", this.spanNode, "><", this.spanNode, " ", this.attrClass, "=\"", this.textClass, "\" ", this.ieUnseletedAble, ">", name, "</", this.spanNode, ">"].join("");
                }
            }
        }
        ///check JSON node type 
        ///if it is object or not
        if (typeof (JSON[item]) == "object") {
            ///HTML for outerHTML of the tree node
            this.MainHTML += ["<", this.ulNode, " ", this.attrClass, "=\"", this.ulClass, "\" ", this.ieUnseletedAble, ">"].join("");

            ///Getting sub node of JSON 
            ///it call itself
            this._getTreeHTML(JSON[item], id);

            ///HTML for outerHTML of the tree node
            this.MainHTML += ["</", this.ulNode, ">"].join("");
        }
    }

    return this.MainHTML;
};
//#endregion

//#region
/// <summary>
/// Generate unique id from pre-id and suggested id
/// </summary>
MainTree.prototype.GetUniqueId = function (preId, newId) {
    ///if the suggested id is blank or undefined
    ///it will suggestion a id
    if (newId == undefined) {
        newId = this.GetUniqueSubId();
    }
    
    ///check the uniqueness of the id
    while (1) {
        ///get id by appending preId and Suggested id
        preId = preId + "." + newId;
        ///get the html dom according to the id
        var checkOBJ = document.getElementById(preId);
        if (checkOBJ != null) {
            ///if htmlDom existed, it will suggested a new id
            newId = this.GetUniqueSubId();
        } else {
            ///if the html dom not found according to the id,
            /// and it return that id
            break;
        }
    }

    return preId;
};
//#endregion

//#region
/// <summary>
/// Generate unique id index
/// </summary>
//TODO: Need to change the index and make those index dynamic
MainTree.prototype.GetUniqueSubId = function () {
    ///update the id index
    this.uniqueIndex++;
    return this.uniqueIndex;
};
//#endregion

